package asapIs134CK.womi.client.games.logoGame.froggitTheFrog.editor;

import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.Cell;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitWorld;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitEnd;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitSand;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitStart;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.cellsImpl.FroggitWall;
import asapIs134CK.womi.client.games.logoGame.utils.Position;
import asapIs134CK.womi.client.games.logoGame.utils.exceptions.MissConstructMapException;
import asapIs134CK.womi.client.games.logoGame.utils.exceptions.ValueException;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

public class MapEditionWidget extends Composite{
	private FlexTable _map;
	private int ySize;
	private int xSize;
	private PickupDragController _dragCtrl;
	
	public MapEditionWidget(PickupDragController dragCtrl){
		_map = new FlexTable();
		_map.setStyleName("froggitTF_tableDebug");
		ySize = 0;
		xSize = 0;
		//TODO init _map with Sand and a 10*10 size (plus 1 colomn and 1 row for the numbers)
		_dragCtrl = dragCtrl;
		initStdMap();
		_map.setVisible(true);
		initWidget(_map);
		
	}

	private void initStdMap() {
		ySize = 10;
		xSize = 10;
		_map.insertRow(0);
		for(int i=1; i <ySize+1; i++){
			_map.insertRow(i);
			_map.setText(i, 0, "r"+i);
		}
		for(int i= 1; i<xSize+1; i++){
			_map.setText(0, i, "c"+i);
		}
		for(int i=1; i<ySize+1; i++){
			for(int j=1;j<xSize+1; j++){
				_map.setWidget(i, j, new MapCell(_dragCtrl));
			}
		}
	}

	public void addARow() {
		ySize++;
		_map.insertRow(ySize);
		_map.setText(ySize, 0, "r"+ySize);
		for(int i = 1; i<=xSize; i++){
			_map.setWidget(ySize, i, new MapCell(_dragCtrl));
		}
	}
	public void addAColumn(){
		xSize++;
		_map.setText(0, xSize, "c"+xSize);
		for(int i = 1; i<=ySize ; i++){
			_map.setWidget(i, xSize, new MapCell(_dragCtrl));
		}
	}

	public void removeLastRow() {
		ySize--;
		_map.removeRow(ySize+1);
	}

	public void removeLastColumn() {
		xSize--;
		for(int i = 0; i<=ySize; i++){
			_map.removeCell(i, xSize+1);
		}
	}
	
	public FroggitWorld getCorrespondingWorld() throws MissConstructMapException{
		Cell[][] mapAvCell = new Cell[ySize][xSize];
		
		boolean startFound = false;
		Position startPos = null;
		boolean endFound = false;
		
		for(int y=1; y<=ySize; y++){
			for(int x=1; x<=xSize; x++){
				Widget w = _map.getWidget(y, x);
				if (w instanceof MapCell) {
					MapCell mapCell = (MapCell) w;
					if (mapCell.getWidget() instanceof CellGUI) {
						CellGUI cellXY = (CellGUI) mapCell.getWidget();
						mapAvCell[y-1][x-1] = cellXY.getCell();
						//now test what is this cell !
						Cell currentCell = mapAvCell[y-1][x-1];
						if (currentCell instanceof FroggitSand || currentCell instanceof FroggitWall){
							//just normal cell nothing to check here...
						}else if(currentCell instanceof FroggitStart){
							if(startFound){
								throw new MissConstructMapException("There is more than one start in your new map ... no allowed");	
							}else{
								//we just found the first Start ! great !
								startFound = true;
								startPos = new Position(x-1, y-1);
							}
						}else if(!endFound && currentCell instanceof FroggitEnd){
							endFound = true;
						}
					}else{
						throw new MissConstructMapException("there is an unreconnized cell in your new Map !\n(p.s. it's at ("+x+","+y+")" +
								"\n\t class = "+mapCell.getWidget().getClass());
					}
				}else{
					throw new MissConstructMapException("there is an unreconnized cell (not even a mapCell ...)");
				}
				
			}
		}
		
		if(!startFound){
			throw new MissConstructMapException("you should have at least one start in your map");
		}
		if(!endFound){
			throw new MissConstructMapException("you should have at least one end in your map");
			
		}
		FroggitWorld newWorld = null;
		try {
			 newWorld = new FroggitWorld(mapAvCell, startPos,"");
		} catch (ValueException e) {
			e.printStackTrace();
			throw new MissConstructMapException("there is a trouble with your map but that seems to come from the programmers ...");
		}
		return newWorld;
	}
}